<?php

require_once 'command/Command.php';
require_once 'command/GetGrosseryListCommand.php';
require_once 'command/GetUserCommand.php';
require_once 'command/CreateUserCommand.php';
require_once 'command/GetProductCommand.php';
require_once 'command/GetAllProductsCommand.php';
require_once 'command/GetTagsCommand.php';
require_once 'command/GetProductsByTagsCommand.php';
require_once 'command/UpdateAmountProductCommand.php';
require_once 'command/DeleteFromListCommand.php';
require_once 'command/AddToListCommand.php';
require_once 'command/GetUserByTagCommand.php';
require_once 'command/UnknownCommand.php';

class Interpreter {

	private $commandArray;

	public function __construct(){
		$this->commandArray = new ArrayObject();
	}

	/**
	 * Decides what to do with a request to this server
	 * Breaks down the XML File and gets the
	 */
	public function interpret($command){
		switch($command){
			case 'getGroceryList':
				$this->commandArray->append(new GetGrosseryListCommand($_POST["id"]));
			break;
			case 'getUser';
				$this->commandArray->append(new GetUserCommand($_POST['name'], $_POST['password']));
			break;
			case 'createUser';
				$this->commandArray->append(new CreateUserCommand($_POST['name'], $_POST['password']));
			break;
			case 'createGroceryList':
				$this->commandArray->append(new CreateGrosseryListCommand($_POST['id'], $_POST['name']));
			break;
			case 'getAllProducts':
				$this->commandArray->append(new GetAllProductsCommand());
			break;
			case 'getProduct':
				$this->commandArray->append(new GetProductCommand($_POST['id']));
			break;
			case 'getTags':
				$this->commandArray->append(new GetTagsCommand($_POST['id']));
			break;
			case 'getProductsByTags':
				$this->commandArray->append(new GetProductsByTagsCommand($_POST['id']));
			break;
			case 'changeAmountProduct':
				$this->commandArray->append(new UpdateAmountProductCommand($_POST['id'], $_POST['productid'], $_POST['amount']));
			break;
			case 'deleteFromList':
				$this->commandArray->append(new DeleteFromListCommand($_POST['id'], $_POST['productid']));
			break;
			case 'addToList':
				$this->commandArray->append(new AddToListCommand($_POST['id'], $_POST['productid'], $_POST['amount']));
			break;
			case 'getUserByTag':
				$this->commandArray->append(new GetUserByTagCommand($_POST['id']));
			break;
			default:
				$this->commandArray->append(new UnknownCommand("No known command"));
			break;
		}
	}

	public function executeCommands(){
		$it = $this->commandArray->getIterator();
		$string = "<?xml version='1.0' encoding='UTF-8'?>";
		while($it->valid()){
			$string .= $it->current()->execute();
			$it->next();
		}
		echo $string;
	}

}